package com.idanchuang.component.logback.converter;


import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.idanchuang.component.logback.appender.DcILoggingEvent;
import com.idanchuang.component.logback.helper.LogContext;
import com.idanchuang.component.logback.util.LogConvertUtil;

public class KeywordPatternConverter extends ClassicConverter {

    @Override
    public String convert(ILoggingEvent le) {
        if (le instanceof DcILoggingEvent) {
            return ((DcILoggingEvent) le).getKeyword();
        }
        return figure(le);
    }

    public static String figure(ILoggingEvent iLoggingEvent) {
        // 如果是组件内部log, 则直接跳过
        if (LogContext.INTERNAL_LOGGER_NAME.equals(iLoggingEvent.getLoggerName())) {
            return "-";
        }
        String keyword = LogContext.getKeyword();
        if (keyword != null && LogConvertUtil.isRollingFileAppender()) {
            LogContext.removeKeyword();
        }
        return keyword != null ? keyword : "-";
    }

}
